package com.smartgame.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Disposable;

public class Assets implements Disposable, AssetErrorListener {
	public static final String TAG = Assets.class.getName();

	public static final Assets instance = new Assets();
	private AssetManager assetManager;

	public AssetFonts fonts;
	public AssetSpiders spiders;
	public AssetBackGroud backgroud;

	public AssetSounds sounds;
	public AssetMusic music;
	
	private Assets() {
	}

	public void init(AssetManager assetManager) {
		this.assetManager = assetManager;
		// set asset manager error handler
		assetManager.setErrorListener(this);
		// load texture atlas
		assetManager.load(Constants.SPIDERS_TEXTURE_ATLAS_PACK,
				TextureAtlas.class);

		// load sounds
		assetManager.load("sounds/sound.wav", Sound.class);
		// load music
		assetManager.load("music/music.mp3", Music.class);

		// start loading assets and wait until finished
		assetManager.finishLoading();
		Gdx.app.debug(TAG,
				"# of assets loaded: " + assetManager.getAssetNames().size);
		for (String a : assetManager.getAssetNames())
			Gdx.app.debug(TAG, "asset: " + a);

		// create game resource objects
		TextureAtlas atlas = assetManager
				.get(Constants.SPIDERS_TEXTURE_ATLAS_PACK);
		// enable texture filtering for pixel smoothing
		for (Texture t : atlas.getTextures()) {
			t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}
		fonts = new AssetFonts();
		spiders = new AssetSpiders(atlas);
		backgroud = new AssetBackGroud();
		
		sounds = new AssetSounds(assetManager);
		music = new AssetMusic(assetManager);
	}

	public class AssetFonts {
		public final BitmapFont defaultSmall;
		public final BitmapFont defaultNormal;
		public final BitmapFont defaultBig;

		public AssetFonts() {
			// create three fonts using Libgdx's 15px bitmap font
			defaultSmall = new BitmapFont(
					Gdx.files.internal("images/arial-15.fnt"), true);
			defaultNormal = new BitmapFont(
					Gdx.files.internal("images/arial-15.fnt"), true);
			defaultBig = new BitmapFont(
					Gdx.files.internal("images/arial-15.fnt"), true);
			// set font sizes
			defaultSmall.setScale(0.75f);
			defaultNormal.setScale(1.0f);
			defaultBig.setScale(2.0f);
			// enable linear texture filtering for smooth fonts
			defaultSmall.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			defaultNormal.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			defaultBig.getRegion().getTexture()
					.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}
	}

	public class AssetSpiders {
		public final Texture animatedTexture;

		public AssetSpiders(TextureAtlas atlas) {
			animatedTexture = new Texture(Gdx.files.internal("images/animation.png"));
		}
	}
	
	public class AssetBackGroud {
		public final TextureRegion reg;

		public AssetBackGroud() {
			// we do not use atlas because it's too big and cannot be packed into a atlas.
			Texture texture = new Texture(Gdx.files.internal("images/background.png"));
			reg = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
		}
	}
	
	public class AssetSounds {
		public final Sound sound;
		
		public AssetSounds (AssetManager am) {
			sound = am.get("sounds/sound.wav", Sound.class);
		}
	}
	
	public class AssetMusic {
		public final Music music;
		public AssetMusic (AssetManager am) {
			music = am.get("music/music.mp3", Music.class);
		}
	}

	@Override
	public void dispose() {
		assetManager.dispose();
	}

	@Override
	@SuppressWarnings("rawtypes")
	public void error(AssetDescriptor asset, Throwable throwable) {
		Gdx.app.error(TAG, "Couldn't load asset '" + asset.fileName + "'",
				(Exception) throwable);
	}

}
